<template>
  <div class="change-box">

    <Card>
      <span slot="title">修改密码</span>
      <div slot="content">
        <el-form :model="editPwdData" status-icon :rules="rules" ref="editPwd" label-width="100px" class="demo-editPwd">
          <!-- 在使用 validate、resetFields 方法的情况下，prop必填 -->
          <el-form-item label="原密码" prop="oldPwd">
            <!-- autocomplete="off" 浏览器不要自动填充 -->
            <el-input v-model="editPwdData.oldPwd" type="password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPwd">
            <el-input type="password" v-model="editPwdData.newPwd" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="renewPwd">
            <el-input type="password" v-model="editPwdData.renewPwd" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </Card>

  </div>
</template>

<script>
import Card from "@/components/Card.vue";
import { PWD_REG, NAM_REG } from "@/utils/reg.js";
import { checkOldPwd_api, editPwd_api } from "@/api/user.js";
import storage from "@/utils/storage.js";
export default {
  components: {
    Card,
  },

  data() {
    var checkOldPwd = async (rule, value, callback) => {
      //像后端发送数据，获得查看原密码是否正确
      if (!value) {
        return callback(new Error("原密码不能为空"));
      } else if (!NAM_REG.test(value)) {
        return callback(new Error("原密码格式错误"));
      } else {
        //判断原密码是否正确
        let res = await checkOldPwd_api({ oldPwd: value });
        if (res.data.code == 11) {
          return callback(new Error("原密码错误"));
        } else {
          callback();
        }
      }
    };

    var checkNewPwd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("新密码不能为空"));
      } else if (!PWD_REG.test(value)) {
        callback(new Error("新密码必须为3-8位"));
      } else if (value === this.editPwdData.oldPwd) {
        callback("新密码不能和原密码一致");
      } else {
        callback();
      }
    };

    var checkRenewPwd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.editPwdData.newPwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      editPwdData: { oldPwd: "", newPwd: "", renewPwd: "" },
      rules: {
        oldPwd: [{ validator: checkOldPwd, trigger: "blur" }],
        newPwd: [{ validator: checkNewPwd, trigger: "blur" }],
        renewPwd: [{ validator: checkRenewPwd, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      //vaild检验是否有未填的输入框
      this.$refs.editPwd.validate(async (valid) => {
        if (valid) {
          //像后端发送新密码
          let res = await editPwd_api({ newPwd: this.editPwdData.newPwd });
          console.log(res.data);
          if (res.data.code == 0) {
            //清除会话存储
            storage.clearSession("tk");
            this.$router.push("/login");
          }
        } else {
          return false;
        }
      });
    },
    //重置表单
    resetForm() {
      this.$refs.editPwd.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-input {
  width: 300px;
}
</style>